package com.ruoyi.financial.service.impl;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.financial.domain.FCar;
import com.ruoyi.financial.domain.FCarCost;
import com.ruoyi.financial.domain.FCarUse;
import com.ruoyi.financial.mapper.FCarCostMapper;
import com.ruoyi.financial.service.IFCarCostService;
import com.ruoyi.financial.service.IFCarService;
import com.ruoyi.system.service.ISysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 费用支出Service业务层处理
 *
 * @author xunzhe
 * @date 2024-03-13
 */
@Service
public class FCarCostServiceImpl implements IFCarCostService {
    @Autowired
    private FCarCostMapper fCarCostMapper;
    @Autowired
    private IFCarService fCarService;
    @Autowired
    private ISysDeptService sysDeptService;
    /**
     * 查询费用支出
     *
     * @param costId 费用支出主键
     * @return 费用支出
     */
    @Override
    public FCarCost selectFCarCostByCostId(Long costId) {
        return fCarCostMapper.selectFCarCostByCostId(costId);
    }

    /**
     * 查询费用支出列表
     *
     * @param fCarCost 费用支出
     * @return 费用支出
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public List<FCarCost> selectFCarCostList(FCarCost fCarCost) {
        return fCarCostMapper.selectFCarCostList(fCarCost);
    }

    /**
     * 新增费用支出
     *
     * @param fCarCost 费用支出
     * @return 结果
     */
    @Override
    public int insertFCarCost(FCarCost fCarCost) {
        fCarCost.setCreateTime(DateUtils.getNowDate());
        return fCarCostMapper.insertFCarCost(fCarCost);
    }

    /**
     * 修改费用支出
     *
     * @param fCarCost 费用支出
     * @return 结果
     */
    @Override
    public int updateFCarCost(FCarCost fCarCost) {
        return fCarCostMapper.updateFCarCost(fCarCost);
    }

    /**
     * 批量删除费用支出
     *
     * @param costIds 需要删除的费用支出主键
     * @return 结果
     */
    @Override
    public int deleteFCarCostByCostIds(String costIds) {
        return fCarCostMapper.deleteFCarCostByCostIds(Convert.toStrArray(costIds));
    }

    /**
     * 删除费用支出信息
     *
     * @param costId 费用支出主键
     * @return 结果
     */
    @Override
    public int deleteFCarCostByCostId(Long costId) {
        return fCarCostMapper.deleteFCarCostByCostId(costId);
    }

    public Map exportData(List<FCarCost> carCostList, List<FCarUse> carUseList) {
        List<Map<String, String>> costList = new ArrayList();
        Map returnMap = new HashMap();
        for (int i = 0; i < 13; i++) {
            int name = i + 1;
            returnMap.put("a" + name, "");
            returnMap.put("b" + name, "");
            returnMap.put("c" + name, "");
            returnMap.put("d" + name, "");
            returnMap.put("e" + name, "");
            returnMap.put("f" + name, "");
            returnMap.put("g" + name, "");
            returnMap.put("h" + name, "");
            returnMap.put("i" + name, "");
            returnMap.put("j" + name, "");//路桥费支出
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        int cqtIndex = 1;
        int cqIndex = 1;
        int cstIndex = 1;
        int csIndex = 1;
        for (FCarCost carCost : carCostList) {
            int name = Integer.parseInt(sdf.format(carCost.getCostTime()).split("-")[1]);
            String index = "";
            if (carCost.getCostType().equals("0")) {
                index = "a";

                String kilometers = returnMap.get("g" + name).toString();
                if (kilometers.equals("")) {
                    if (carCost.getKilometers() != null && !carCost.getKilometers().equals("")) {
                        kilometers = carCost.getKilometers();
                    }
                } else {
                    if (carCost.getKilometers() != null && !carCost.getKilometers().equals("")) {
                        kilometers = Double.parseDouble(kilometers) + Double.parseDouble(carCost.getKilometers()) + "";
                    }
                }
                returnMap.put("g" + name, kilometers.replaceAll("\\.0",""));

                String petrol = returnMap.get("h" + name).toString();
                if (petrol.equals("")) {
                    if (carCost.getPetrol() != null && !carCost.getPetrol().equals("")) {
                        petrol = carCost.getPetrol();
                    }
                } else {
                    if (carCost.getPetrol() != null && !carCost.getPetrol().equals("")) {
                        petrol = Double.parseDouble(petrol) + Double.parseDouble(carCost.getPetrol()) + "";
                    }
                }
                returnMap.put("h" + name, petrol.replaceAll("\\.0",""));

                String zongkilometers = returnMap.get("g13").toString();
                String zongpetrol = returnMap.get("h13").toString();
                if(!carCost.getKilometers().equals("")){
                    if(zongkilometers.equals("")){
                        zongkilometers = carCost.getKilometers();
                    }else{
                        zongkilometers = Double.parseDouble(zongkilometers) + Double.parseDouble(carCost.getKilometers())+"";
                    }
                }
                if(!carCost.getPetrol().equals("")){
                    if(zongpetrol.equals("")){
                        zongpetrol = carCost.getPetrol();
                    }else{
                        zongpetrol = Double.parseDouble(zongpetrol) + Double.parseDouble(carCost.getPetrol())+"";
                    }
                }
                returnMap.put("g13", zongkilometers.replaceAll("\\.0",""));
                returnMap.put("h13", zongpetrol.replaceAll("\\.0",""));

            } else if (carCost.getCostType().equals("1")) {
                index = "b";
            } else if (carCost.getCostType().equals("2")) {
                index = "c";
            } else if (carCost.getCostType().equals("3")) {
                index = "d";
            } else if (carCost.getCostType().equals("4")) {
                index = "e";
            } else if (carCost.getCostType().equals("5")) {
                index = "f";
            }else if(carCost.getCostType().equals("8")){
                index = "j";
            }else if(carCost.getCostType().equals("6")){
                //保险
                String qiangStartTime = "";
                if(carCost.getQiangInsureSatrtTime()!=null){
                    String format = sdf.format(carCost.getQiangInsureSatrtTime());
                    String[] split = format.split("-");
                    qiangStartTime = split[0]+"年"+split[1]+"月"+split[2]+"日";
                }
                if(carCost.getQiangInsureEndTime()!=null){
                    String format = sdf.format(carCost.getQiangInsureEndTime());
                    String[] split = format.split("-");
                    qiangStartTime = qiangStartTime + "至" + split[0]+"年"+split[1]+"月"+split[2]+"日";
                }
                String qiangCompany = "";
                String qiangCost = "";
                if(carCost.getQiangInsureCompany()!=null){
                    qiangCompany = carCost.getQiangInsureCompany();
                }
                if(carCost.getQiangCost()!=null){
                    qiangCost = carCost.getQiangCost();
                }
                returnMap.put("qiangStartTime",qiangStartTime);
                returnMap.put("qiangCompany",qiangCompany);
                returnMap.put("qiangCost",qiangCost);

                String shangStartTime = "";
                if(carCost.getShangInsureSatrtTime()!=null){
                    String format = sdf.format(carCost.getShangInsureSatrtTime());
                    String[] split = format.split("-");
                    shangStartTime = split[0]+"年"+split[1]+"月"+split[2]+"日";
                }
                if(carCost.getShangInsureEndTime()!=null){
                    String format = sdf.format(carCost.getShangInsureEndTime());
                    String[] split = format.split("-");
                    shangStartTime = shangStartTime + "至" + split[0]+"年"+split[1]+"月"+split[2]+"日";
                }
                String shangCompany = "";
                String shangCost = "";
                if(carCost.getShangInsureCompany()!=null){
                    shangCompany = carCost.getShangInsureCompany();
                }
                if(carCost.getShangCost()!=null){
                    shangCost = carCost.getShangCost();
                }
                returnMap.put("shangStartTime",shangStartTime);
                returnMap.put("shangCompany",shangCompany);
                returnMap.put("shangCost",shangCost);

            }else if(carCost.getCostType().equals("7")){
                //出险
                if(carCost.getChuQiang()!=null){
                    String format = sdf.format(carCost.getChuQiang());
                    String[] split = format.split("-");
                    returnMap.put("cqt"+cqtIndex,split[1]+"月"+split[2]);
                    cqtIndex++;
                }
                if(carCost.getChuQiangCost()!=null){
                    returnMap.put("cq"+cqIndex,carCost.getChuQiangCost());
                    cqIndex++;
                }
                if(carCost.getChuShang()!=null){
                    String format = sdf.format(carCost.getChuShang());
                    String[] split = format.split("-");
                    returnMap.put("cst"+cstIndex,split[1]+"月"+split[2]);
                    cstIndex++;
                }
                if(carCost.getChuShangCost()!=null){
                    returnMap.put("cs"+csIndex,carCost.getChuShangCost());
                    csIndex++;
                }
            }

            if(!index.equals("") && carCost.getCost()!=null && !carCost.getCost().equals("")){
                String cost = returnMap.get(index + name).toString();
                if (cost.equals("")) {
                    cost = carCost.getCost();
                } else {
                    cost = Double.parseDouble(cost) + Double.parseDouble(carCost.getCost()) + "";
                }
                returnMap.put(index + name, cost.replaceAll("\\.0",""));

                if(carCost.getCost()!=null && !carCost.getCost().equals("")){
                    String zongcost = returnMap.get(index+"13").toString();
                    if(zongcost.equals("")){
                        zongcost = carCost.getCost();
                    }else{
                        zongcost = Double.parseDouble(zongcost) + Double.parseDouble(carCost.getCost())+"";
                    }
                    returnMap.put(index+"13", zongcost.replaceAll("\\.0",""));
                }
            }
        }

        for (int i = 0; i < 13; i++) {
            int name = i + 1;
            String a = returnMap.get("a" + name).toString();
            String b = returnMap.get("b" + name).toString();
            String c = returnMap.get("c" + name).toString();
            String d = returnMap.get("d" + name).toString();
            String e = returnMap.get("e" + name).toString();
            String f = returnMap.get("f" + name).toString();
            String j = returnMap.get("j" + name).toString();

            double zong = 0;
            if(!a.equals("")){
                zong = Double.parseDouble(a) + zong;
            }
            if(!b.equals("")){
                zong = Double.parseDouble(b) + zong;
            }
            if(!c.equals("")){
                zong = Double.parseDouble(c) + zong;
            }
            if(!d.equals("")){
                zong = Double.parseDouble(d) + zong;
            }
            if(!e.equals("")){
                zong = Double.parseDouble(e) + zong;
            }
            if(!f.equals("")){
                zong = Double.parseDouble(f) + zong;
            }
            if(!j.equals("")){
                zong = Double.parseDouble(j) + zong;
            }
            if(zong==0){
                returnMap.put("i"+name, "");
            }else{
                returnMap.put("i"+name, (zong+"").replaceAll("\\.0",""));
            }
        }

        //出行记录
        Long useIndex = 1L;
        String zongGongLi = "";
        String zongPrice = "";
        String zongJiayouPrice = "";
        String zongTingchePrice = "";
        String zongWeixiuPrice = "";
        String zongQitaPrice = "";
        for(FCarUse carUse : carUseList){
            if(carUse.getTotalNumber()!=null && !carUse.getTotalNumber().equals("")){
                if(zongGongLi.equals("")){
                    zongGongLi = carUse.getTotalNumber();
                }else{
                    zongGongLi = Double.parseDouble(zongGongLi) + Double.parseDouble(carUse.getTotalNumber())+"";
                }
            }
            if(carUse.getPrice()!=null && !carUse.getPrice().equals("")){
                if(zongPrice.equals("")){
                    zongPrice = carUse.getPrice();
                }else{
                    zongPrice = Double.parseDouble(zongPrice) + Double.parseDouble(carUse.getPrice())+"";
                }
            }
            if(carUse.getJiayouPrice()!=null && !carUse.getJiayouPrice().equals("")){
                if(zongJiayouPrice.equals("")){
                    zongJiayouPrice = carUse.getJiayouPrice();
                }else{
                    zongJiayouPrice = Double.parseDouble(zongJiayouPrice) + Double.parseDouble(carUse.getJiayouPrice())+"";
                }
            }
            if(carUse.getTingchePrice()!=null && !carUse.getTingchePrice().equals("")){
                if(zongTingchePrice.equals("")){
                    zongTingchePrice = carUse.getTingchePrice();
                }else{
                    zongTingchePrice = Double.parseDouble(zongTingchePrice) + Double.parseDouble(carUse.getTingchePrice())+"";
                }
            }
            if(carUse.getWeixiuPrice()!=null && !carUse.getWeixiuPrice().equals("")){
                if(zongWeixiuPrice.equals("")){
                    zongWeixiuPrice = carUse.getWeixiuPrice();
                }else{
                    zongWeixiuPrice = Double.parseDouble(zongWeixiuPrice) + Double.parseDouble(carUse.getWeixiuPrice())+"";
                }
            }
            if(carUse.getQitaPrice()!=null && !carUse.getQitaPrice().equals("")){
                if(zongQitaPrice.equals("")){
                    zongQitaPrice = carUse.getQitaPrice();
                }else{
                    zongQitaPrice = Double.parseDouble(zongQitaPrice) + Double.parseDouble(carUse.getQitaPrice())+"";
                }
            }
            carUse.setUseId(useIndex);
            useIndex++;
        }
        returnMap.put("useList",carUseList);
        returnMap.put("zongGongLi",zongGongLi.replaceAll("\\.0",""));
        returnMap.put("zongPrice",zongPrice.replaceAll("\\.0",""));
        returnMap.put("zongJiayouPrice",zongJiayouPrice.replaceAll("\\.0",""));
        returnMap.put("zongTingchePrice",zongTingchePrice.replaceAll("\\.0",""));
        returnMap.put("zongWeixiuPrice",zongWeixiuPrice.replaceAll("\\.0",""));
        returnMap.put("zongQitaPrice",zongQitaPrice.replaceAll("\\.0",""));

        //车基本信息
        Long carId = null;
        if(carCostList.size()>0){
            carId = carCostList.get(0).getCarId();
        }else if(carUseList.size()>0){
            carId = carUseList.get(0).getCarId();
        }
        if(carId!=null){
            FCar fCar = fCarService.selectFCarByCarId(carId);
            if(fCar.getCarNumber()!=null){
                returnMap.put("carNumber",fCar.getCarNumber());
            }
            if(fCar.getCarModel()!=null){
                returnMap.put("carModel",fCar.getCarModel());
            }
            if(fCar.getCarColour()!=null){
                returnMap.put("carColour",fCar.getCarColour());
            }
            if(fCar.getFadongji()!=null){
                returnMap.put("fadongji",fCar.getFadongji());
            }
            if(fCar.getChejiahao()!=null){
                returnMap.put("chejiahao",fCar.getChejiahao());
            }
            if(fCar.getPailiang()!=null){
                returnMap.put("pailiang",fCar.getPailiang());
            }
            if(fCar.getPurchaseTime()!=null){
                returnMap.put("purchaseTime",fCar.getPurchaseTime());
            }
            if(fCar.getPurchasePrice()!=null){
                returnMap.put("purchasePrice",fCar.getPurchasePrice());
            }
            if(fCar.getTingfangweizhi()!=null){
                returnMap.put("tingfangweizhi",fCar.getTingfangweizhi());
            }
            if(fCar.getJiashiyuan()!=null){
                returnMap.put("jiashiyuan",fCar.getJiashiyuan());
            }
            if(fCar.getJiashiyuanNumber()!=null){
                returnMap.put("jiashiyuanNumber",fCar.getJiashiyuanNumber());
            }
            if(fCar.getZerenren()!=null){
                returnMap.put("zerenren",fCar.getZerenren());
            }
            if(fCar.getCreateBy()!=null){
                returnMap.put("createBy",fCar.getCreateBy());
            }
            if(fCar.getCreateTime()!=null){
                returnMap.put("createTime",fCar.getCreateTime());
            }
            if(fCar.getDeptId()!=null){
                SysDept sysDept = sysDeptService.selectDeptById(fCar.getDeptId());
                returnMap.put("deptName",sysDept.getDeptName());
            }
        }

        return returnMap;
    }


}
